package org.database.filter.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.database.filter.model.NhaDauTuDoanhNghiep;
import org.database.filter.model.PersionType;
import org.database.filter.model.ProjectID;
import org.database.filter.model.TuNhan;
import org.database.filter.service.TextParserService;
import org.springframework.stereotype.Service;

@Service
public class TextParserServiceImpl implements TextParserService {
	private static final String SEMI_COLON = ";";
	private static final String CMND_KEY_WORD_2 = "do";
	private static final String BIRTHDAY_KEY_WORD = "sinh";
	private static final String CMND_KEY_WORD = "hộ";
	private static final String SLASH = "/";
	private static final String DATE_KEY_WORD = "ngày";
	private static final String SO_KEY_WORD = "số";
	private static final String GIAY_KEY_WORD = "giấy";
	private static final String COLON = ",";
	private static final String DOT = "\\.";
	public String[] nameKeys = { "ông", "bà", "mr", "mrs" };
	public String[] addressKeys = { "thường trú", "tạm trú", "tại", "địa chỉ" };
	public String[] CMNDKeys = { "hộ chiếu", "chứng minh", "cmnd", CMND_KEY_WORD_2, SO_KEY_WORD };
	public String[] birthDayKeys = { "sinh ngày", "ngày sinh", BIRTHDAY_KEY_WORD };
	public String[] nationalityKeys = { "quốc tịch", "quốc", "nước", "người" };
	public String[] projectPrefix = { "GP", "GC" };

	public TuNhan parseTextPerson(String text, Integer ma_cq, String ad) {
		TuNhan person = new TuNhan();
		if (text == null || text.trim().isEmpty()) {
			return null;
		} else if (text.trim().length() < 4) {
			return null;
		}
		String[] sentencesWithComma = text.split(COLON);
		List<String> sentences = new ArrayList<String>();
		if (sentencesWithComma != null) {
			for (String sentence : sentencesWithComma) {
				if (sentence != null) {
					if (sentence.contains(SEMI_COLON)) {
						for (String sen : sentence.split(SEMI_COLON)) {
							sentences.add(sen);
						}
					} else {
						sentences.add(sentence);
					}
				}
			}
		}
		PersionType lastValue = null;
		boolean hasCMND = false;
		for (int i = 0; i < sentences.size(); i++) {
			String sentence = sentences.get(i);
			boolean hasValue = false;
			if (i == 0) {
				if (sentence.indexOf(BIRTHDAY_KEY_WORD) != -1) {
					sentence = sentence.substring(0, sentence.indexOf(BIRTHDAY_KEY_WORD));
				}
				if (sentence.indexOf(CMND_KEY_WORD) != -1) {
					sentence = sentence.substring(0, sentence.indexOf(CMND_KEY_WORD));
				}
				if (sentence.indexOf(SO_KEY_WORD) != -1) {
					sentence = sentence.substring(0, sentence.indexOf(SO_KEY_WORD));
				}
				person.setTEN(sentence);
				lastValue = PersionType.NAME;
				hasValue = true;
				continue;
			}
			for (String key : CMNDKeys) {
				if (hasCMND) {
					break;
				}
				if (sentence.toLowerCase().contains(key)) {
					if (sentence.contains(CMND_KEY_WORD_2)) {
						String lastWord = "";
						for (String word : sentence.split(" ")) {
							if (word.equals(CMND_KEY_WORD_2)) {
								person.setCMND_PASSPORT(lastWord);
								hasValue = true;
								lastValue = PersionType.CMND;
								hasCMND = true;
								break;
							} else {
								lastWord = word;
							}
						}
					} else if (sentence.contains(SO_KEY_WORD)) {
						boolean next = false;
						for (String word : sentence.split(" ")) {
							if (next) {
								person.setCMND_PASSPORT(word);
								hasValue = true;
								lastValue = PersionType.CMND;
								hasCMND = true;
								break;
							}
							if (word.equals(SO_KEY_WORD)) {
								next = true;
							}
						}
					}
					if (!hasCMND && sentence != null && sentence.length() > 19) {
						person.setCMND_PASSPORT(sentence.substring(0, 19));
					}
				}

			}
			for (String key : addressKeys) {
				if (sentence.toLowerCase().contains(key)) {
					person.setCHO_O_HIEN_NAY(sentence);
					lastValue = PersionType.ADDRESS;
					hasValue = true;
					break;
				}
			}

			for (String key : nationalityKeys) {
				if (sentence.toLowerCase().contains(key)) {
					person.setHO_KHAU(sentence);
					lastValue = PersionType.NATIONALITY;
					hasValue = true;
					break;
				}
			}
			if (!hasValue) {
				switch (lastValue) {
				case NAME:
					if (sentence.indexOf(BIRTHDAY_KEY_WORD) != -1) {
						sentence = sentence.substring(0,
								sentence.indexOf(BIRTHDAY_KEY_WORD));
					}
					if (sentence.indexOf(CMND_KEY_WORD) != -1) {
						sentence = sentence
								.substring(0, sentence.indexOf(CMND_KEY_WORD));
					}
					if (sentence.indexOf(SO_KEY_WORD) != -1) {
						sentence = sentence
								.substring(0, sentence.indexOf(SO_KEY_WORD));
					}
					person.setTEN(person.getTEN() + sentence);
					break;
				case ADDRESS:
					person.setCHO_O_HIEN_NAY(person.getCHO_O_HIEN_NAY()
							+ sentence);
					break;
				default:
					break;
				}
			}
		}
		if (person.getCMND_PASSPORT() != null
				&& (person.getCHO_O_HIEN_NAY() == null || person
						.getCHO_O_HIEN_NAY().trim().isEmpty()) && ad != null
				&& !ad.isEmpty()) {
			person.setCHO_O_HIEN_NAY(ad);
		}
		return person;
	}

	public NhaDauTuDoanhNghiep parseTextDN(String text, Integer ma_cq) {
		NhaDauTuDoanhNghiep person = new NhaDauTuDoanhNghiep();
		if (ma_cq != null) {
			person.setMA_QG(ma_cq);
		}
		String[] sentences = text.split(COLON);
		if (sentences.length == 1) {
			sentences = text.split(DOT);
		}
		PersionType lastValue = null;
		for (int i = 0; i < sentences.length; i++) {
			boolean hasValue = false;
			String sentence = sentences[i];
			if (i == 0) {
				person.setTENDN(sentence);
				lastValue = PersionType.NAME;
				hasValue = true;
				continue;
			}
			for (String key : addressKeys) {
				if (sentence.toLowerCase().contains(key)) {
					person.setDIACHI(sentence);
					lastValue = PersionType.ADDRESS;
					hasValue = true;
					continue;
				}
			}

			if (!hasValue) {
				switch (lastValue) {
				case NAME:
					if (sentence.indexOf(GIAY_KEY_WORD) != -1) {
						sentence = sentence.substring(0,
								sentence.indexOf(GIAY_KEY_WORD));
					}
					if (sentence.indexOf(SO_KEY_WORD) != -1) {
						sentence = sentence
								.substring(0, sentence.indexOf(SO_KEY_WORD));
					}
					if (sentence.indexOf(DATE_KEY_WORD) != -1) {
						sentence = sentence.substring(0,
								sentence.indexOf(DATE_KEY_WORD));
					}
					person.setTENDN(person.getTENDN() + sentence);
					break;
				case ADDRESS:
					person.setDIACHI(person.getDIACHI() + sentence);
					break;
				default:
					break;
				}
			}
		}
		return person;
	}

	public ProjectID getListProjectID(String text) {
		ProjectID projectID = new ProjectID();
		List<String> ids = new ArrayList<String>();
		for (String word : text.split(" ")) {
			for (String key : projectPrefix) {
				if (word.contains(key)) {
					ids.add(word.replace(COLON, ""));
				}
			}
		}
		projectID.setListProjectID(ids);
		if (ids.size() > 0) {
			projectID.setPrefixNumber(Integer
					.parseInt(ids.get(0).split(SLASH)[0]));
		}
		return projectID;
	}
}
